---
title: "Replication material for: Precarious work and challenger parties: how precarity influenced vote choice in the 2018 Italian election"
author: "Elisabetta Girardi"
date: '09/07/2023'
output: html_document
---

```{r setup, include=FALSE}
rm(list = ls())
gc()

ipak <- function(pkg) {
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

Packages <- c("rio", "haven", "janitor", "stargazer", "foreign", "magrittr",
              "nnet", "MASS", "MNLpred", "ggplot2", "ggplot2", "extrafont", "scales", "effects", "ggpubr",
              "jtools", "DAMisc", "simEd")

ipak(Packages)


set.seed(260423)

```

For the analysis, I rely on survey data collected by ITANES in occasion of the 2018 Italian election. 
The dataset has been downloaded from http://www.itanes.org/dati/ 
The questionaire is available at the following link: file:///C:/Users/egiradi/Desktop/PhD/Article_M5S/Itanes_2018_pre_post_datafile/Itanes_2018_release01_post_electoral_questionario.pdf
In what follows, I upload the data and prepare them for the analysis by recoding and relabeling the variables of interest. 
I then save the dataset (ITANES2018_ready) on which I conduct the analysis. 

```{r, include=FALSE} 
setwd("") #add working directory
ITANES2018 <- read.spss("Itanes_2018_release01_panel_pre_post.sav", to.data.frame = TRUE)  # Reading data

```

age 
```{r, include=FALSE}
ITANES2018$age<-ITANES2018$dem02
table(ITANES2018$age, useNA="always")
is.numeric(ITANES2018$age)
```

gender 
```{r, include=FALSE}
table(ITANES2018$dem01)
ITANES2018$gender<-ifelse(ITANES2018$dem01=="Donna",1,0)
```

education 
```{r, include=FALSE}
#Education
ITANES2018$education<-ITANES2018$dem09
table(ITANES2018$education,useNA="always")
ITANES2018$education<-as.numeric(as.factor(ITANES2018$education))
table(ITANES2018$education, ITANES2018$dem09)
```

region of residence 
```{r, include=FALSE}
ITANES2018$residence<-ITANES2018$dem07
table(ITANES2018$residence, useNA="always")
levels(ITANES2018$residence)

levels(ITANES2018$residence)[1:2]<-"North"
levels(ITANES2018$residence)
levels(ITANES2018$residence)[3:4]<-"South e Isole"
levels(ITANES2018$residence)


ITANES2018$North<-ITANES2018$residence
levels(ITANES2018$North)
levels(ITANES2018$North)[1]<-1
levels(ITANES2018$North)[2:3]<-0
levels(ITANES2018$North)
ITANES2018$North<-as.numeric(levels(ITANES2018$North))[ITANES2018$North]
table(ITANES2018$North, ITANES2018$residence)


ITANES2018$Center<-ITANES2018$residence
levels(ITANES2018$Center)
levels(ITANES2018$Center)[1]<-0
levels(ITANES2018$Center)[2]<-1
levels(ITANES2018$Center)[3]<-0
levels(ITANES2018$Center)
ITANES2018$Center<-as.numeric(levels(ITANES2018$Center))[ITANES2018$Center]
table(ITANES2018$Center, ITANES2018$residence)

ITANES2018$South<-ITANES2018$residence
levels(ITANES2018$South)
levels(ITANES2018$South)[1:2]<-0
levels(ITANES2018$South)[2]<-1
levels(ITANES2018$South)
ITANES2018$South<-as.numeric(levels(ITANES2018$South))[ITANES2018$South]
table(ITANES2018$South, ITANES2018$residence)
```

union membership
```{r, include=FALSE}
ITANES2018$union<-ITANES2018$sind1_post
levels(ITANES2018$union)
levels(ITANES2018$union)[3]<-NA
ITANES2018$union<-ifelse(ITANES2018$union=="Sì",1,0 )
table(ITANES2018$union)
```

ideology
```{r, include=FALSE}
ITANES2018$ideology<-ITANES2018$lrself
levels(ITANES2018$ideology)
#I code "I do not know"  as NA
ITANES2018$ideology[ITANES2018$ideology== "Non saprei"] <- NA
table(ITANES2018$ideology)

#And I drop unused levels
ITANES2018$ideology <- factor(ITANES2018$ideology)
levels(ITANES2018$ideology)
#Merge: 0-3 Left; 4: Centre-left; 5: Centre; 6: Centre-right; 7-10 Right
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="Sinistra"] <- "Left"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="1"] <- "Left"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="2"] <- "Left"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="3"] <- "Centre-Left"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="4"] <- "Centre-Left"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="5"] <- "Centre"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="6"] <- "Centre-Right"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="7"] <- "Centre-Right"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="8"] <- "Right"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="9"] <- "Right"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="Destra"] <- "Right"
levels(ITANES2018$ideology)[levels(ITANES2018$ideology)=="Non mi colloco da nessuna parte"] <- "None"
levels(ITANES2018$ideology)

ITANES2018$ideology<-factor(ITANES2018$ideology, levels = c("None", "Left", "Centre-Left", "Centre", "Centre-Right", "Right"))


ITANES2018$left<-ifelse(ITANES2018$ideology=="Left", 1,0)
table(ITANES2018$left, ITANES2018$ideology)

ITANES2018$centre_left<-ifelse(ITANES2018$ideology=="Centre-Left", 1,0)
table(ITANES2018$centre_left, ITANES2018$ideology)

ITANES2018$centre<-ifelse(ITANES2018$ideology=="Centre", 1,0)
table(ITANES2018$centre, ITANES2018$ideology)

ITANES2018$centre_right<-ifelse(ITANES2018$ideology=="Centre-Right", 1,0)
table(ITANES2018$centre_right, ITANES2018$ideology)

ITANES2018$right<-ifelse(ITANES2018$ideology=="Right", 1,0)
table(ITANES2018$right, ITANES2018$ideology)

ITANES2018$none<-ifelse(ITANES2018$ideology=="None", 1,0)
table(ITANES2018$none, ITANES2018$ideology)


ITANES2018$ideology2<-ITANES2018$ideology
levels(ITANES2018$ideology2)


```

attitudes towards the European Union 
```{r, include=FALSE}
table(ITANES2018$euro1)
ITANES2018$euro<-ITANES2018$euro1
ITANES2018$euro[ITANES2018$euro== "Non saprei"] <- NA
ITANES2018$euro <- factor(ITANES2018$euro)
levels(ITANES2018$euro)
ITANES2018$euro<-factor(ITANES2018$euro, levels = c("Un male", "Né un bene né un male", "Un bene"))
table(ITANES2018$euro, ITANES2018$euro1)

ITANES2018$euro<-as.numeric(as.factor(ITANES2018$euro))
table(ITANES2018$euro,ITANES2018$euro1)
is.numeric(ITANES2018$euro)
#Higher values indicate higher support
```

attitudes towards immigration
```{r, include=FALSE}
ITANES2018$immigration<-ITANES2018$img3
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="Una minaccia per la cultura italiana"] <- 0
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="1"] <- 1
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="2"] <- 2
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="3"] <- 3
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="4"] <- 4
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="5"] <- 5
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="6"] <- 6
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="7"] <- 7
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="8"] <-8
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="9"] <-9
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="Una arricchimento per la cultura italiana"] <- 10
levels(ITANES2018$immigration)
levels(ITANES2018$immigration)[levels(ITANES2018$immigration)=="Non saprei"] <- NA
ITANES2018$immigration<-factor(ITANES2018$immigration)
ITANES2018$immigration<-as.numeric(levels(ITANES2018$immigration))[ITANES2018$immigration] 
#Lower vlues indicate opposition to immigration

table(ITANES2018$img3, ITANES2018$immigration)
mean(ITANES2018$immigration, na.rm=TRUE)
```

populist attitudes 
```{r, include=FALSE}
ITANES2018$populism<-ITANES2018$itanes7pop2
table(ITANES2018$populism)
ITANES2018$populism<- factor(ITANES2018$populism, levels = c("Per niente d’accordo", "Poco d’accordo", "Né d’accordo né in disaccordo", "Abbastanza d’accordo", "Molto d’accordo"))
table(ITANES2018$populism, ITANES2018$itanes7pop2)
ITANES2018$populism<-as.numeric(as.factor(ITANES2018$populism))
table(ITANES2018$populism, ITANES2018$itanes7pop2)
#Higher values indicate more populism (molto d'accordo che i cittadini, e non i politici, dovrebbero prendere le decisioni politiche più importanti)
```

trust (in political parties)
```{r, include=FALSE}
ITANES2018$trust<-ITANES2018$fiducia2_post
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)==" Nessuna fiducia"] <- 0
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)=="1"] <- 1
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)=="2"] <- 2
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)=="3"] <- 3
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)=="4"] <- 4
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)=="5"] <- 5
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)=="6"] <- 6
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)=="7"] <- 7
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)=="8"] <-8
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)=="9"] <-9
levels(ITANES2018$trust)
levels(ITANES2018$trust)[levels(ITANES2018$trust)==" Completa Fiducia"] <- 10
levels(ITANES2018$trust)
ITANES2018$trust<-as.numeric(levels(ITANES2018$trust))[ITANES2018$trust]
table(ITANES2018$trust, ITANES2018$fiducia2_post)
```

economic hardship 
```{r, include=FALSE}
ITANES2018$eco_hardship<-ITANES2018$dem21
table(ITANES2018$eco_hardship)
levels(ITANES2018$eco_hardship)
levels(ITANES2018$eco_hardship)[5]<-NA
levels(ITANES2018$eco_hardship)

ITANES2018$eco_hardship_dummy<-NA
ITANES2018$eco_hardship_dummy<-ifelse(ITANES2018$eco_hardship=="Con qualche difficoltà"|ITANES2018$eco_hardship=="Non riesco ad arrivare a fine mese", 1,0)
table(ITANES2018$eco_hardship_dummy, ITANES2018$eco_hardship)

ITANES2018$eco_hardship<-as.numeric(as.factor(ITANES2018$eco_hardship))
#Higher values indicate higher economic hardship
```

turnout
```{r, include=FALSE}
#Vote vs abstention
table(ITANES2018$voto1_post)
ITANES2018$turnout<-ITANES2018$voto1_post
table(ITANES2018$turnout, useNA="always")
#I code "I'd rather not answer" as NA
levels(ITANES2018$turnout)[3] <- NA
levels(ITANES2018$turnout)
ITANES2018$turnout<-ifelse(ITANES2018$turnout=="Sì", 1,0)
table(ITANES2018$turnout)
```

vote choice
```{r, include=FALSE}
ITANES2018$voto<-ITANES2018$votocheck_post
table(ITANES2018$voto)
levels(ITANES2018$voto)[1] <- "Other parties"
levels(ITANES2018$voto)
levels(ITANES2018$voto)[2:3] <- "Mainstream-left"
levels(ITANES2018$voto)
levels(ITANES2018$voto)[3] <- "Challenger-left"
levels(ITANES2018$voto)
levels(ITANES2018$voto)[4] <- "Mainstream-right"
levels(ITANES2018$voto)
levels(ITANES2018$voto)[5:6] <- "Challenger-right"
levels(ITANES2018$voto)
levels(ITANES2018$voto)[6] <- "Mainstream-right"
levels(ITANES2018$voto)
levels(ITANES2018$voto)[6:7] <-"Mainstream-left"
levels(ITANES2018$voto)
levels(ITANES2018$voto)[6] <-"Other parties"
levels(ITANES2018$voto)
levels(ITANES2018$voto)[6:7] <-"No party"
levels(ITANES2018$voto)
levels(ITANES2018$voto)[7] <-NA
levels(ITANES2018$voto)
ITANES2018$voto <- factor(ITANES2018$voto, levels = c("Mainstream-left","Mainstream-right","Challenger-left", "Challenger-right", "Other parties", "No party"))
table(ITANES2018$voto,ITANES2018$votocheck_post)

#voto with League as a mainstream party
ITANES2018$voto_Lega<-ITANES2018$votocheck_post
table(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[1] <- "Other parties"
levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[2:3] <- "Mainstream-left"
levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[3] <- "Challenger-left"
levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[4:5] <- "Mainstream-right"
levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[5] <- "Challenger-right"
levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[6] <- "Mainstream-right"
levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[6:7] <-"Mainstream-left"
levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[6] <-"Other parties"
levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[6:7] <-"No party"
levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[7] <-NA
levels(ITANES2018$voto_Lega)
ITANES2018$voto_Lega <- factor(ITANES2018$voto_Lega, levels = c("Mainstream-left","Mainstream-right","Challenger-left", "Challenger-right", "Other parties", "No party"))
table(ITANES2018$voto_Lega,ITANES2018$votocheck_post)

levels(ITANES2018$voto_Lega)
levels(ITANES2018$voto_Lega)[5:6]<-NA


ITANES2018$voto_abstain<-NA
ITANES2018$voto_abstain<-ifelse(ITANES2018$voto=="No party"&ITANES2018$turnout==1, "Abstain", ITANES2018$voto_abstain)
table(ITANES2018$voto_abstain)
ITANES2018$voto_abstain<-ifelse(is.na(ITANES2018$voto)==TRUE&ITANES2018$turnout==0, "Abstain", ITANES2018$voto_abstain)
table(ITANES2018$voto_abstain)
ITANES2018$voto_abstain<-ifelse(ITANES2018$voto=="Other parties"&ITANES2018$turnout==1, "Other parties", ITANES2018$voto_abstain)
table(ITANES2018$voto_abstain)
ITANES2018$voto_abstain<-ifelse(ITANES2018$voto=="Mainstream-left"&ITANES2018$turnout==1, "Mainstream-left", ITANES2018$voto_abstain)
table(ITANES2018$voto_abstain)
ITANES2018$voto_abstain<-ifelse(ITANES2018$voto=="Challenger-left"&ITANES2018$turnout==1, "Challenger-left", ITANES2018$voto_abstain)
table(ITANES2018$voto_abstain)
ITANES2018$voto_abstain<-ifelse(ITANES2018$voto=="Mainstream-right"&ITANES2018$turnout==1, "Mainstream-right", ITANES2018$voto_abstain)
ITANES2018$voto_abstain<-ifelse(ITANES2018$voto=="Challenger-right"&ITANES2018$turnout==1, "Challenger-right", ITANES2018$voto_abstain)
table(ITANES2018$voto_abstain)

ITANES2018$voto<-ITANES2018$voto_abstain
ITANES2018$voto<-factor(ITANES2018$voto)
levels(ITANES2018$voto)[1]<-NA
levels(ITANES2018$voto)
ITANES2018$voto <- factor(ITANES2018$voto, levels = c("Mainstream-left","Mainstream-right","Challenger-left", "Challenger-right", "Other parties"))

ITANES2018$voto_abstain <- factor(ITANES2018$voto_abstain, levels = c("Abstain", "Mainstream-left","Mainstream-right","Challenger-left", "Challenger-right", "Other parties"))
table(ITANES2018$voto_abstain,ITANES2018$voto)


ITANES2018$voto_challenger<-ITANES2018$voto
levels(ITANES2018$voto_challenger)
levels(ITANES2018$voto_challenger)[1:2]<-"Mainstream"
levels(ITANES2018$voto_challenger)
levels(ITANES2018$voto_challenger)[4]<-NA

ITANES2018$voto_mainstream<-ITANES2018$voto
levels(ITANES2018$voto_mainstream)
levels(ITANES2018$voto_mainstream)[3:4]<-"Challenger"
levels(ITANES2018$voto_mainstream)
levels(ITANES2018$voto_mainstream)[4]<-NA
ITANES2018$voto_mainstream<-factor(ITANES2018$voto_mainstream, levels= c("Challenger", "Mainstream-left", "Mainstream-right"))


ITANES2018$voto_Lega_challenger<-ITANES2018$voto_Lega
levels(ITANES2018$voto_Lega_challenger)
levels(ITANES2018$voto_Lega_challenger)[1:2]<-"Mainstream"
levels(ITANES2018$voto_Lega_challenger)
levels(ITANES2018$voto_Lega_challenger)[4]<-NA

ITANES2018$voto_Lega_mainstream<-ITANES2018$voto
levels(ITANES2018$voto_Lega_mainstream)
levels(ITANES2018$voto_Lega_mainstream)[3:4]<-"Challenger"
levels(ITANES2018$voto_Lega_mainstream)
levels(ITANES2018$voto_Lega_mainstream)[4]<-NA
ITANES2018$voto_Lega_mainstream<-factor(ITANES2018$voto_Lega_mainstream, levels= c("Challenger", "Mainstream-left", "Mainstream-right"))

levels(ITANES2018$voto)
levels(ITANES2018$voto)[5]<-NA
```

voto2013 and voto2013_abstain
```{r, include=FALSE}
ITANES2018$voto2013<-ITANES2018$voto2013_post
table(ITANES2018$voto2013, useNA="always")
levels(ITANES2018$voto2013)
levels(ITANES2018$voto2013)[10:97] <- NA
levels(ITANES2018$voto2013)
levels(ITANES2018$voto2013)[11] <- NA
levels(ITANES2018$voto2013)


levels(ITANES2018$voto2013)
levels(ITANES2018$voto2013)[1:2]<-"Challenger-left"
levels(ITANES2018$voto2013)
levels(ITANES2018$voto2013)[2]<-"Mainstream left"
levels(ITANES2018$voto2013)
levels(ITANES2018$voto2013)[3]<-"Challenger-left"
levels(ITANES2018$voto2013)
levels(ITANES2018$voto2013)[3:5]<-"Mainstream right"
levels(ITANES2018$voto2013)
levels(ITANES2018$voto2013)[4:5]<-"Challenger-right"
levels(ITANES2018$voto2013)
levels(ITANES2018$voto2013)[5]<-"Abstain"
table(ITANES2018$voto2013)

table(ITANES2018$voto2013, ITANES2018$voto2013_post)
ITANES2018$voto2013<-factor(ITANES2018$voto2013, levels=c("Abstain" , "Challenger-left","Mainstream left", "Mainstream right", "Challenger-right"))
levels(ITANES2018$voto2013)

ITANES2018$voto_abstain_2013<-ITANES2018$voto2013
levels(ITANES2018$voto2013)
levels(ITANES2018$voto2013)[1]<-NA

table(ITANES2018$voto2013, ITANES2018$voto_abstain_2013, useNA="always")

```

Attitudes towards the incumbent
```{r, include=FALSE}
ITANES2018$incumbent<-ITANES2018$itanes2
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)==" Completamente negativo"] <- 0
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)=="1"] <- 1
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)=="2"] <- 2
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)=="3"] <- 3
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)=="4"] <- 4
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)=="5"] <- 5
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)=="6"] <- 6
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)=="7"] <- 7
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)=="8"] <-8
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)=="9"] <-9
levels(ITANES2018$incumbent)
levels(ITANES2018$incumbent)[levels(ITANES2018$incumbent)==" Completamento positivo"] <- 10
levels(ITANES2018$incumbent)
ITANES2018$incumbent<-as.numeric(levels(ITANES2018$incumbent))[ITANES2018$incumbent]
table(ITANES2018$incumbent,ITANES2018$itanes2)
#from completamente negativo (1) to completamente positivo (10)

ITANES2018$incumbent_yesno<-ITANES2018$incumbent
ITANES2018$incumbent_yesno<-as.factor(as.numeric(ITANES2018$incumbent_yesno))
levels(ITANES2018$incumbent_yesno)
levels(ITANES2018$incumbent_yesno)[1:5]<-"No"

levels(ITANES2018$incumbent_yesno)[2]<-"medium"

levels(ITANES2018$incumbent_yesno)[3:8]<-"Yes"



```


*Independent variable: employment status*
unemployed
```{r, include=FALSE}
ITANES2018$unemployed<-ITANES2018$dem18
levels(ITANES2018$unemployed)
levels(ITANES2018$unemployed)[4:6]<-"unemployed"
levels(ITANES2018$unemployed)
levels(ITANES2018$unemployed)[7]<-"unemployed"
levels(ITANES2018$unemployed)
ITANES2018$unemployed<-ifelse(ITANES2018$unemployed=="unemployed", 1, 0)
```

dependent_autonomous
```{r, include=FALSE}
table(ITANES2018$dem13)
ITANES2018$dependent_autonomous<-ITANES2018$dem13
table(ITANES2018$dependent_autonomous)
```

contract
```{r, include=FALSE}
table(ITANES2018$dem16)
ITANES2018$contract<-ITANES2018$dem16
levels(ITANES2018$contract)
#I code "I do not know" and "I'd rather not answer" as NA
levels(ITANES2018$contract)[1] <- "Permanently employed"
levels(ITANES2018$contract)
levels(ITANES2018$contract)[2:4] <- "Atypically employed"
levels(ITANES2018$contract)
levels(ITANES2018$contract)[3:6] <- NA
table(ITANES2018$contract)
```

```{r, include=FALSE}
ITANES2018$employment_status<-NA

ITANES2018$employment_status<- ifelse(ITANES2018$contract=="Permanently employed" & is.na(ITANES2018$unemployed) == TRUE ,  "Permanently employed", ITANES2018$employment_status)

table(ITANES2018$employment_status)

ITANES2018$employment_status<- ifelse(ITANES2018$contract=="Atypically employed" & is.na(ITANES2018$unemployed) == TRUE ,  "Atypically employed", ITANES2018$employment_status)

table(ITANES2018$employment_status)

ITANES2018$employment_status<- ifelse(is.na(ITANES2018$contract)==TRUE & ITANES2018$unemployed ==1,  "Unemployed", ITANES2018$employment_status)

table(ITANES2018$employment_status)

ITANES2018$employment_status<- ifelse(is.na(ITANES2018$contract)==TRUE & is.na(ITANES2018$unemployed) == TRUE & ITANES2018$dependent_autonomous=="Autonomo",  "Selfemployed", ITANES2018$employment_status)

levels(ITANES2018$employment_status)

ITANES2018$employment_status<-as.factor(as.character(ITANES2018$employment_status))
ITANES2018$employment_status<-factor(ITANES2018$employment_status, levels=c("Selfemployed" , "Permanently employed","Atypically employed","Unemployed"))


#DUMMIES
ITANES2018$permanently_employed<-ifelse(ITANES2018$employment_status=="Permanently employed",1,0)
table(ITANES2018$permanently_employed)

ITANES2018$atypically_employed<-ifelse(ITANES2018$employment_status=="Atypically employed",1,0)
table(ITANES2018$atypically_employed)

ITANES2018$unemployed<-ifelse(ITANES2018$employment_status=="Unemployed",1,0)
table(ITANES2018$unemployed)

ITANES2018$selfemployed<-ifelse(ITANES2018$employment_status=="Selfemployed",1,0)
table(ITANES2018$selfemployed)

ITANES2018$employment_status_nounemp<-ITANES2018$employment_status
levels(ITANES2018$employment_status_nounemp)[4]<-NA
```

**Perceived precarity**

Fear of job loss among active workers 
```{r, include=FALSE}

table(ITANES2018$dem22)
ITANES2018$fear_activeworkers<- ITANES2018$dem22
levels(ITANES2018$fear_activeworkers)
levels(ITANES2018$fear_activeworkers)[4] <- NA
table(ITANES2018$fear_activeworkers)
#And I drop unused levels
ITANES2018$fear_activeworkers <- factor(ITANES2018$fear_activeworkers)
levels(ITANES2018$fear_activeworkers)
ITANES2018$fear_activeworkers<- factor(ITANES2018$fear_activeworkers, levels = c("No, non ho avuto nessuna paura di perdere il lavoro", "Si, ho avuto un po’ di paura di perdere il lavoro","Si, ho avuto molta paura di perdere il lavoro"))


ITANES2018$fear_activeworkers_dummy<-ITANES2018$fear_activeworkers
ITANES2018$fear_activeworkers_dummy<-ifelse(ITANES2018$fear_activeworkers=="Si, ho avuto un po’ di paura di perdere il lavoro"|ITANES2018$fear_activeworkers=="Si, ho avuto molta paura di perdere il lavoro", 1, 0)
table(ITANES2018$fear_activeworkers_dummy, ITANES2018$unemployed)
table(ITANES2018$fear_activeworkers_dummy, ITANES2018$dem22)#check

ITANES2018$fear_activeworkers<-as.numeric(as.factor(ITANES2018$fear_activeworkers))

```

fear of job loss including the unemployed (dummy)
```{r, include=FALSE}
ITANES2018$fear_all_dummy<- NA

ITANES2018$fear_all_dummy <- ifelse (ITANES2018$unemployed ==1& is.na(ITANES2018$fear_activeworkers_dummy) == TRUE, "Afraid", ITANES2018$fear_all_dummy)

table(ITANES2018$fear_all_dummy)

ITANES2018$fear_all_dummy <- ifelse (ITANES2018$unemployed ==0 & ITANES2018$fear_activeworkers_dummy == 1, "Afraid", ITANES2018$fear_all_dummy)

table(ITANES2018$fear_all_dummy)

ITANES2018$fear_all_dummy <- ifelse (ITANES2018$unemployed ==0 & ITANES2018$fear_activeworkers_dummy ==0,"Not afraid", ITANES2018$fear_all_dummy)

table(ITANES2018$fear_all_dummy)

ITANES2018$fear_all_dummy_numeric<-ifelse(ITANES2018$fear_all_dummy=="Afraid", 1, 0)
table(ITANES2018$fear_all_dummy, ITANES2018$fear_all_dummy_numeric)

ITANES2018$fear_all_dummy<-factor(ITANES2018$fear_all_dummy, levels = c("Not afraid", "Afraid"))
```

fear of job loss including the unemployed 
```{r, include=FALSE}
ITANES2018$fear_all<- NA

ITANES2018$fear_all<-ifelse(ITANES2018$dem22=="No, non ho avuto nessuna paura di perdere il lavoro", "No, non ho avuto nessuna paura di perdere il lavoro", ITANES2018$fear_all)
table(ITANES2018$fear_all)
ITANES2018$fear_all<-ifelse(ITANES2018$dem22=="Si, ho avuto un po’ di paura di perdere il lavoro", "Si, ho avuto un po’ di paura di perdere il lavoro", ITANES2018$fear_all)
table(ITANES2018$fear_all)
ITANES2018$fear_all<-ifelse(ITANES2018$dem22=="Si, ho avuto molta paura di perdere il lavoro", "Si, ho avuto molta paura di perdere il lavoro", ITANES2018$fear_all)
table(ITANES2018$fear_all)
ITANES2018$fear_all<-ifelse(is.na(ITANES2018$dem22)==TRUE & ITANES2018$unemployed==1, "disoccupato", ITANES2018$fear_all)
table(ITANES2018$fear_all)
ITANES2018$fear_all<- factor(ITANES2018$fear_all, levels = c("No, non ho avuto nessuna paura di perdere il lavoro", "Si, ho avuto un po’ di paura di perdere il lavoro","Si, ho avuto molta paura di perdere il lavoro", "disoccupato"))
table(ITANES2018$fear_all)

ITANES2018$fear_all<-as.numeric(as.factor(ITANES2018$fear_all))
table(ITANES2018$fear_all)
```

Save final dataset
```{r}
ITANES2018_ready<-ITANES2018
save(ITANES2018_ready, file="ITANES2018_ready.RData")
```


